
**************************************************************
** authors: Nadeau & Daoust					 				**
** title: Maslow				 					    **
** data: CSES 1 & 4								  	        **
** date: December 2019 								      	**
**************************************************************

set more off

*Load Data
use "/Users/jfd/Desktop/Maslow/electoral studies submissiono/R&R electoral studies/final documents of r1/Pooledcses4&1&2&3.dta", clear
 cd "/Users/jfd/Desktop/"
 

**************************************************************
** 					Preparation							   	**
**************************************************************

*Merge the 2 Belgium 1999
replace D1003=5611999 if D1003==5621999
replace ELECID=5611999 if ELECID==5621999	

*Recode Regime (correct USA 1996)
replace Regime=1 if ELECID==84001996 

*Drop modules 2 and 3
drop if Module==2 | Module==3

*Add data for Taiwan 
replace lnGDPppppc=.2 if D1003==15801996
replace lnGDPppppc=.58 if D1003==15802012

replace CGDPPPPpc=4.4  if D1003==15801996
replace CGDPPPPpc=3.1  if D1003==15802012


**************************************************************
** 					Generating variables				    **
**************************************************************

*Generate ID
egen ELEC_ID= group(ELECID)
egen COUNTRY_ID=group(Pays)

*Generate Age groups
gen age_groups=1 if Age < 35
replace age_groups=2 if Age > 34 & Age < 55
replace age_groups=3 if Age > 54

gen young=0
replace young=1 if age_groups==1 
gen old=0
replace old=1 if age_groups==3

gen a18_44=0
replace a18_44=1 if Age < 45
gen a45_plus=0
replace a45_plus=1 if Age > 44

gen a18_49=0
replace a18_49=1 if Age < 50
gen a50_plus=0
replace a50_plus=1 if Age > 49

*Categorical variable of winner
gen winner_loser_abstain=0 if Losers5==0 & Winners5==0
replace winner_loser_abstain=1 if Losers5==1
replace winner_loser_abstain=2 if Winners5==1

*Note: Mexico election
replace winner_loser_abstain=winner_loser_largest if Pays==29

*Variable keeping only the fourth election when the country is surveyed four time (mexico)
gen four_times=Pays1elecSuiv
replace four_times=0 if Pays==29 & ELECYEAR != 2015

*SWD using 4 categories 
gen Satisfaction_ologit=.
replace Satisfaction_ologit=1 if Satisfaction==0
replace Satisfaction_ologit=2 if Satisfaction==0.33
replace Satisfaction_ologit=3 if Satisfaction==0.67
replace Satisfaction_ologit=4 if Satisfaction==1

**************************************************************
** 						Label variables				 		**
**************************************************************
 
label variable Age01 "Age"
label variable Genre "Sex (Man=1)"
label variable Education01 "Education"
label variable Revenu01 "Income"
label variable Gagnant "Voted for the winner"
label variable Gallagher01 "Gallagher index"
label variable Economy "Economic perceptions"
label variable GDPpc201 "GDP per capita"
label variable DIFFVOTE "Political efficacy"
label variable LTDEM301 "Age of regime"
label variable Corruption01 "Corruption"
label variable Regime "Presidential"
label variable Scrutin "Majoritarian"
label variable GINI01 "Gini"
label variable GDPpc2 "GDP per capita"
	
**************************************************************
** 				Tables and Figures in the MS	 			**
**************************************************************
	
*Figure 1 -- preparation	

tempname 72_dots 
postfile `72_dots' ELEC_ID Economy DIFFVOTE using 72_dots.dta , replace

	forval i = 1/72  {
	reg Satisfaction Age01 Genre Education01 Revenu01 ib0.winner_loser_abstain Economy DIFFVOTE if ELEC_ID==`i' 
	post `72_dots' (`i')  (`=_b[Economy]')  (`=_b[DIFFVOTE]') 
	}
	
	postclose `72_dots' 
	use 72_dots , clear
	list 

*Merge the .dta in the replication files (generated using a simple collapse of GDP by ELECID) to the .dta used for Fig 1.
merge 1:1 ELEC_ID using "lnGDP_ppp_tomerge.dta"



*Figure 1 
twoway (scatter Economy lnGDPppppc) (lfit Economy lnGDPppppc, lcolor(black)), ytitle("Coefficients") ///
		xtitle("Logged GDP (ppp) per capita") ylabel(-.1(.1).4) legend(position(6) row(1)) ||  ///
		(scatter DIFFVOTE lnGDPppppc, symbol(triangle) mfcolor(white) color(black)) (lfit DIFFVOTE lnGDPppppc, lpattern(solid) lcolor(black)) 
	


	
	
	
*Go back to the main .dta and re-run preparation lines (1-103)

*Table 1
eststo clear

eststo t1 : mixed   Satisfaction DIFFVOTE Economy Age01 Genre Education01 Revenu01  ib0.winner_loser_abstain  lnGDPppppc CGDPPPPpc  ///
					Scrutin GINI01 Corruption01 || ELEC_ID: 
eststo t2 : mixed   Satisfaction c.lnGDPppppc##c.Economy  c.lnGDPppppc##c.DIFFVOTE  DIFFVOTE  Economy Age01 Genre Education01 Revenu01 ib0.winner_loser_abstain lnGDPppppc CGDPPPPpc  ///
					Scrutin GINI01 Corruption01 || ELEC_ID: Economy DIFFVOTE
					
esttab ///
       t1 t2  ///
       using "Maslow_Table1.rtf", ///
	   label mlabels(,titles) nogap compress pr2 r2 b(2) se(2) nonumbers interaction(" X ")   replace
	
margins, dydx(Economy DIFFVOTE) at(lnGDPppppc=(0(0.1)0.7)) post
marginsplot, recastci(rarea) ysize(5) ylabel(0(0.05)0.25) legend(position(6) row(1))  ///
			xlabel (0(0.1)0.7) xtitle("log of GDP (ppp) per capita") ytitle("Average marginal effects", size(small)) title(" ")
	


**************************************************************
** 					SUPPLEMENTARY MATERIAL			 		**
**************************************************************

**********************************************
** 		Different indicators of wealth		**
**********************************************
eststo clear

eststo a1 : mixed   Satisfaction c.GDPPPPpc01##c.Economy  c.GDPPPPpc01##c.DIFFVOTE  DIFFVOTE  Economy Age01 Genre Education01 Revenu01   ib0.winner_loser_abstain  CGDPPPPpc  ///
					 Scrutin GINI01 Corruption01 || ELEC_ID: Economy DIFFVOTE
margins, dydx(Economy DIFFVOTE) at(GDPPPPpc01=(0(0.1)1)) post
marginsplot , recastci(rarea) ciopt(color(%20)) ysize(5) ylabel(0(0.05)0.2) legend(position(6) row(1)) graphregion(color(white)) xtitle("GDP (ppp) per capita") ///
			xlabel (0(0.1)1) ytitle("Average marginal effect", size(small)) title(" ")
graph save a1.gph, replace

eststo a2 : mixed   Satisfaction c.GDPpc201##c.Economy  c.GDPpc201##c.DIFFVOTE  DIFFVOTE  Economy Age01 Genre Education01 Revenu01  ib0.winner_loser_abstain CGDP  ///
					 Scrutin GINI01 Corruption01 || ELEC_ID: Economy DIFFVOTE
margins, dydx(Economy DIFFVOTE) at(GDPpc201=(0(0.1)1)) post
marginsplot , recastci(rarea) ciopt(color(%20)) ysize(5) ylabel(0(0.05)0.2) legend(position(6) row(1)) graphregion(color(white)) xtitle("GDP per capita") ///
			ytitle("Average marginal effect", size(small)) title(" ") xlabel (0(0.1)1)
graph save a2.gph, replace

eststo a3 : mixed   Satisfaction c.lnGDPpc201##c.Economy  c.lnGDPpc201##c.DIFFVOTE  DIFFVOTE  Economy Age01 Genre Education01 Revenu01 ib0.winner_loser_abstain  CGDP  ///
					 Scrutin GINI01 Corruption01 || ELEC_ID: Economy DIFFVOTE	
margins, dydx(Economy DIFFVOTE) at(lnGDPpc201=(0(0.1)0.7)) post
marginsplot , recastci(rarea)  ciopt(color(%20)) ysize(5) ylabel(0(0.05)0.2) legend(position(6) row(1)) graphregion(color(white)) xtitle("Log of GDP per capita") ///
			xlabel(0(0.1)0.7) ytitle("Average marginal effect", size(small)) title(" ")
graph save a3.gph, replace
					
										
esttab ///
       a1 a2 a3  ///
       using "TableSM2.rtf", ///
	   label mlabels(,titles) nogap compress b(2) se(2) nonumbers interaction(" X ")   replace
	   
graph combine a1.gph a2.gph a3.gph, col(1) ysize(12) xsize(7)


**********************************************
** 			Estimation techniques			**
**********************************************
*Three levels model
eststo a1 : mixed   Satisfaction c.lnGDPppppc##c.Economy  c.lnGDPppppc##c.DIFFVOTE  DIFFVOTE  Economy Age01 Genre Education01 Revenu01  ib0.winner_loser_abstain   CGDPPPPpc   ///
					 Scrutin GINI01 Corruption01 || COUNTRY_ID: || ELEC_ID: Economy DIFFVOTE 
margins, dydx(Economy DIFFVOTE) at(lnGDPppppc=(0(0.1)0.7)) post
marginsplot , recastci(rarea)  ciopt(color(%20))  ysize(5) ylabel(0(0.05)0.2) legend(position(6) row(1)) graphregion(color(white)) xtitle("Log of GDP (ppp) per capita") ///
			xlabel(0(0.1)0.7) ytitle("Average marginal effect", size(small)) title("Three-levels mixed effects linear regression")
graph save a1.gph, replace

*Mixed effects ordered logit 
eststo a2 : meologit   Satisfaction_ologit c.lnGDPppppc##c.Economy  c.lnGDPppppc##c.DIFFVOTE  DIFFVOTE  Economy Age01 Genre Education01 Revenu01  ib0.winner_loser_abstain   CGDPPPPpc   ///
					 Scrutin GINI01 Corruption01  || ELEC_ID: Economy DIFFVOTE
margins, dydx(DIFFVOTE) at(lnGDPppppc=(0(0.1)0.7)) 
marginsplot , recastci(rarea)  ciopt(color(%20)) ysize(5)  legend(position(6) row(1)) graphregion(color(white)) xtitle("Log of GDP (ppp) per capita") ///
			xlabel (0(0.1)0.7) ytitle("Average marginal effect of political efficacy", size(small)) title("Mixed effects ordered logistic regression")
graph save a2.gph, replace	

esttab ///
       a1 a2 ///
       using "TableSM3.rtf", ///
	   label mlabels(,titles) nogap compress  b(2) se(2) nonumbers interaction(" X ")   replace

graph combine a1.gph a2.gph, col(1) ysize(10) xsize(8)

************************************************
** 			By democratic quality			**
************************************************
*VDem subsamples 
eststo a1 : mixed   Satisfaction c.lnGDPppppc##c.Economy  c.lnGDPppppc##c.DIFFVOTE  DIFFVOTE  Economy Age01 Genre Education01 Revenu01  ib0.winner_loser_abstain  CGDPPPPpc  ///
					 Scrutin GINI01 Corruption01 || ELEC_ID: Economy DIFFVOTE if VDem01 >.5
margins, dydx(Economy DIFFVOTE) at(lnGDPppppc=(0(0.1)0.7)) post
marginsplot , xlabel(0(0.1)0.7) recastci(rarea) ciopt(color(%20))  ysize(5) ylabel(0(0.05)0.2) legend(position(6) row(1)) graphregion(color(white)) xtitle("Log of GDP (ppp) per capita") ///
			ytitle("Average marginal effect", size(small)) title("V-Dem > .5")
graph save a1.gph, replace

*Freedom house < 3 (i.e. free. = 1 to 2.5)
eststo a2 : mixed   Satisfaction c.lnGDPppppc##c.Economy  c.lnGDPppppc##c.DIFFVOTE  DIFFVOTE  Economy Age01 Genre Education01 Revenu01   ib0.winner_loser_abstain  CGDPPPPpc  ///
					 Scrutin GINI01 Corruption01 || ELEC_ID: Economy DIFFVOTE if FreeHouse <3
margins, dydx(Economy DIFFVOTE) at(lnGDPppppc=(0(0.1)0.7)) post
marginsplot , xlabel(0(0.1)0.7) recastci(rarea)  ciopt(color(%20))  ysize(5) ylabel(0(0.05)0.2) legend(position(6) row(1)) graphregion(color(white)) xtitle("Log of GDP (ppp) per capita") ///
			ytitle("Average marginal effect", size(small)) title("Freedom House = Free")
graph save a2.gph, replace

esttab ///
       a1 a2  ///
       using "Maslow_TableSM4.rtf", ///
	   label mlabels(,titles) nogap compress b(2) se(2) nonumbers interaction(" X ")   replace
	   
graph combine a1.gph a2.gph, col(1) ysize(10) xsize(8)

**********************************************
** 				By Module					**
**********************************************

*Module 1
eststo a1 : mixed   Satisfaction c.lnGDPppppc##c.Economy  c.lnGDPppppc##c.DIFFVOTE  DIFFVOTE  Economy Age01 Genre Education01 Revenu01   ib0.winner_loser_abstain  CGDPPPPpc  ///
					 Scrutin GINI01 Corruption01 || ELEC_ID: Economy DIFFVOTE if Module==1
margins, dydx(Economy DIFFVOTE) at(lnGDPppppc=(0(0.1)0.7)) post
marginsplot , recastci(rarea) ciopt(color(%20)) xlabel(0(0.1)0.7)  note("")  ysize(5) ylabel(0(0.05)0.2) legend(position(6) row(1)) graphregion(color(white)) ///
				xtitle("Log of GDP (ppp) per capita") ytitle("Average marginal effect", size(small)) title("Module 1 only")
graph save a1.gph, replace

*Module 4
eststo a2 : mixed   Satisfaction c.lnGDPppppc##c.Economy  c.lnGDPppppc##c.DIFFVOTE  DIFFVOTE  Economy Age01 Genre Education01 Revenu01   ib0.winner_loser_abstain  CGDPPPPpc  ///
					 Scrutin GINI01 Corruption01 || ELEC_ID: Economy DIFFVOTE if Module==4
margins, dydx(Economy DIFFVOTE) at(lnGDPppppc=(0(0.1)0.7)) post
marginsplot , recastci(rarea) ciopt(color(%20))  xlabel(0(0.1)0.7)  ysize(5) ylabel(0(0.05)0.2) legend(position(6) row(1)) graphregion(color(white)) xtitle("Log of GDP (ppp) per capita") ///
			ytitle("Average marginal effect", size(small)) title("Module 4 only")
graph save a2.gph, replace

esttab ///
       a1 a2  ///
       using "TableSM5.rtf", ///
	   label mlabels(,titles) nogap compress b(2) se(2) nonumbers interaction(" X ")   replace
	   
graph combine a1.gph a2.gph, col(1) ysize(10) xsize(8)
	   
**********************************************
** 			More or less covariates			**
**********************************************

*Without much controls
eststo a1 : mixed   Satisfaction c.lnGDPppppc##c.Economy  c.lnGDPppppc##c.DIFFVOTE  DIFFVOTE  Economy  ///
					 || ELEC_ID: Economy DIFFVOTE 
margins, dydx(Economy DIFFVOTE) at(lnGDPppppc=(0(0.1)0.7)) post					 
marginsplot , recastci(rarea) ciopt(color(%20)) ysize(5) ylabel(0(0.05)0.2) legend(position(6) row(1)) graphregion(color(white))  xtitle("Log of GDP (ppp) per capita") ///
			xlabel (0(0.1)0.7) ytitle("Average marginal effect", size(small)) title("No control")
graph save a1, replace

eststo a2 : mixed   Satisfaction c.lnGDPppppc##c.Economy  c.lnGDPppppc##c.DIFFVOTE  DIFFVOTE  Economy ib0.winner_loser_abstain  CGDPPPPpc ///
					 || ELEC_ID: Economy DIFFVOTE  // necessary micro control only
margins, dydx(Economy DIFFVOTE) at(lnGDPppppc=(0(0.1)0.7)) post					 
marginsplot , recastci(rarea) ciopt(color(%20)) ysize(5) ylabel(0(0.05)0.2) legend(position(6) row(1)) graphregion(color(white)) xtitle("Log of GDP (ppp) per capita") ///
			xlabel (0(0.1)0.7) ytitle("Average marginal effect", size(small)) title("Micro-level controls")
graph save a2, replace
			
eststo a3 : mixed   Satisfaction c.lnGDPppppc##c.Economy  c.lnGDPppppc##c.DIFFVOTE  DIFFVOTE  Economy GINI01 Corruption01 ///
					 || ELEC_ID: Economy DIFFVOTE // necessary macro control only
margins, dydx(Economy DIFFVOTE) at(lnGDPppppc=(0(0.1)0.7)) post					 				 
marginsplot , recastci(rarea) ciopt(color(%20)) ysize(5) ylabel(0(0.05)0.2) legend(position(6) row(1)) graphregion(color(white)) xtitle("Log of GDP (ppp) per capita") ///
			xlabel (0(0.1)0.7) ytitle("Average marginal effect", size(small)) title("Macro-level controls")
graph save a3, replace
					 		
			
			
esttab ///
      a1 a2 a3 ///
       using "TableSM6.rtf", ///
	   label mlabels(,titles) nogap compress b(2) se(2) nonumbers interaction(" X ")   replace
	
graph combine a1.gph a2.gph a3.gph,col(1) ysize(12) xsize(7)

**********************************************
** 				By age groups				**
**********************************************					
eststo a1 : mixed   Satisfaction c.lnGDPppppc##c.Economy  c.lnGDPppppc##c.DIFFVOTE  DIFFVOTE  Economy Age01 Genre Education01 Revenu01 ib0.winner_loser_abstain lnGDPppppc CGDPPPPpc  ///
					Scrutin GINI01 Corruption01 || ELEC_ID: Economy DIFFVOTE	if a18_44==0
margins, dydx(Economy DIFFVOTE) at(lnGDPppppc=(0(0.1)0.7)) post					 				 
marginsplot , recastci(rarea) ysize(5) ylabel(0(0.05)0.2) legend(position(6) row(1)) graphregion(color(white)) xtitle("Log of GDP (ppp) per capita") ///
			xlabel (0(0.1)0.7) ciopt(color(%20)) ytitle("Average marginal effect", size(small)) title("18 to 44 years old")
graph save a1, replace	
		
eststo a2 : mixed   Satisfaction c.lnGDPppppc##c.Economy  c.lnGDPppppc##c.DIFFVOTE  DIFFVOTE  Economy Age01 Genre Education01 Revenu01 ib0.winner_loser_abstain lnGDPppppc CGDPPPPpc  ///
					Scrutin GINI01 Corruption01 || ELEC_ID: Economy DIFFVOTE	if a18_44==1	
margins, dydx(Economy DIFFVOTE) at(lnGDPppppc=(0(0.1)0.7)) post					 				 
marginsplot , recastci(rarea) ciopt(color(%20)) ysize(5) ylabel(0(0.05)0.2) legend(position(6) row(1)) graphregion(color(white)) xtitle("Log of GDP (ppp) per capita") ///
			xlabel (0(0.1)0.7) ytitle("Average marginal effect", size(small)) title("45+ years old")
graph save a2, replace				
			
esttab ///
      a1 a2   ///
       using "TableSM7.rtf", ///
	   label mlabels(,titles) nogap compress  b(2) se(2) nonumbers interaction(" X ")   replace
					
graph combine a1.gph a2.gph, col(1) ysize(10) xsize(8)			
					

	
					
